package com.gray.pool.general;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created on 2019/6/23.
 *
 * @author Grak
 * @since 1.0
 */
@RestController
public class ControllerTest {

    @Autowired
    @Qualifier("sampleExecutor")
    ThreadPoolExecutor sampleExecutor;

    @Autowired
    @Qualifier("executorService")
    ThreadPoolExecutor executorService;

    @RequestMapping("/sample")
    public void sample(){
        for(int i = 0;i< 41;i++){
            Runnable runnable = () -> {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Thread thread = Thread.currentThread();
                System.out.println("使用线程池:" + thread.getName() + "---" + thread.getId());
            };
            sampleExecutor.submit(runnable);
        }
    }

    @RequestMapping("/executor")
    public void executor(){
        for(int i = 0;i< 100;i++){
            Runnable runnable = () -> {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Thread thread = Thread.currentThread();
                System.out.println("使用线程池:" + thread.getName() + "---" + thread.getId());
            };
            executorService.submit(runnable);
        }
    }
}
